<?php
include '../newlib.php';
include '../lib/AppScope.pre.php';
include 'header.php';
$theUser = $app->theUser;
$stmSelect = $tmpConnection->prepare("select * from images where image=:filename");
$stmInsert = $tmpConnection->prepare("insert into images (image) values (:filename)");
$systemImages = array("left.gif", "right.gif", "stop.gif", "stoplink.gif");
$listStart = false;
echo '<h1>Afbeeldingen inventariseren</h1>';
if (is_dir($dir = "../images/")) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
        	if (is_file($dir . $file)) {
            	$stmSelect->execute(array(':filename' => $file));
				$rs = $stmSelect->fetch(PDO::FETCH_ASSOC);
				if (!$rs) {
					if (!in_array($file, $systemImages)) {
						$stmInsert->execute(array(':filename' => $file));
						if (!$listStart) {
							echo '<ol>';
							$listStart = true;
						}
						echo "<li><b>$file</b> in database opgenomen</li>";
					}
				}
				if (!is_file($dir . "www/" . $file) || !is_file($dir . "thumbs/" . $file)) {
					if (!$listStart) {
						echo '<ol>';
						$listStart = true;
					}
					$im = new Imagick($dir . $file);
					$im->thumbnailImage(496, 280);
					$im->writeImage($dir .  "thumbs/" . $file);
					
					echo "<li><b>$file</b> als thumbnail 496x280 voor website aangemaakt</li>";
					if (!is_file($dir . "www/" . $file)) {
						copy($dir .  "thumbs/" . $file, $dir . "www/" . $file);
						echo "<li><b>$file</b> klaargezet voor upload</li>";
					}
				}
        	}
        }
        closedir($dh);
    }
}
if ($listStart) {
	echo '</ol>';
} else {
	echo '<p>Afbeeldingendatabase is up-to-date.</p>';
}
include 'footer.php';
?>